package com.masterclient.app.modules.purchase.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.purchase.PurchaseCartItemBean;
import com.eoner.baselibrary.bean.purchase.PurchaseCartProductItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.widget.CenterImageSpan;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.widget.TagTextView;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hjian
 * @date 2020/9/20.
 */
public class PurchaseShopCartAdapter extends BaseQuickAdapter<PurchaseCartProductItemBean, BaseViewHolder> {

    private OnPurchaseShopCartListener onPurchaseShopCartListener;

    public interface OnPurchaseShopCartListener {
        void onMainGoodsCheck(String productId, boolean isSelected);

        void onSkuGoodsCheck(String productId, String carId, boolean isSelected);

        void onSkuReduce(PurchaseCartItemBean itemBean);

        void onSkuAdd(PurchaseCartItemBean itemBean);

        void onEditClick(PurchaseCartItemBean itemBean);
    }

    public PurchaseShopCartAdapter() {
        super(R.layout.item_purchase_shop_cart);
    }

    public void setOnPurchaseShopCartListener(OnPurchaseShopCartListener onPurchaseShopCartListener) {
        this.onPurchaseShopCartListener = onPurchaseShopCartListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, PurchaseCartProductItemBean item) {
        TagTextView tagTextView = helper.getView(R.id.item_purchase_cart_name);
        LinearLayout viewSkuContainer = helper.getView(R.id.ll_sku_container);
        FrameLayout viewMainCheck = helper.getView(R.id.item_purchase_cart_selected_layout);
        ImageView ivMainCheck = helper.getView(R.id.item_purchase_cart_selected_icon);

        ivMainCheck.setImageResource(item.getIsSelected() ? R.mipmap.dealer_radio_select : R.mipmap.dealer_radio_default);
        ViewTransformUtil.glideImageView(mContext, item.getMain_image(), helper.getView(R.id.item_purchase_cart_image), new Transformation[]{new CenterCrop(),
                new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0)}, R.mipmap.defaults);
//        List<String> tagList = new ArrayList<>();
//        tagList.add(item.getMain_desc());
//        tagTextView.setContentAndTag((tagList.size() > 0 ? " " : "") + item.getMain_name(), tagList, false, true);
//        tagTextView.setMaxLines(2);
        if (!TextUtils.isEmpty(item.getMain_desc())){
            setTextTag(tagTextView, item.getMain_desc(), item.getMain_name());
        }else {
            tagTextView.setText(item.getMain_name());
        }
        initSkuInfo(item, viewSkuContainer);

        viewMainCheck.setOnClickListener(v -> {
            onPurchaseShopCartListener.onMainGoodsCheck(item.getProduct_main_id(), !item.getIsSelected());
        });
    }

    private void setTextTag(TextView tv, String strTag, String content){
        StringBuffer buffer = new StringBuffer(strTag);
        buffer.append(content);
        SpannableString spannableString = new SpannableString(buffer.toString());
        TextView tvTag = new TextView(mContext);
        tvTag.setText(strTag);
        tvTag.setTextSize(10);
        tvTag.setTextColor(mContext.getResources().getColor(R.color.white));
        tvTag.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ScreenUtils.dp2px(14)));
        tvTag.setGravity(Gravity.CENTER);
        tvTag.setIncludeFontPadding(false);
        tvTag.setPadding(ScreenUtils.dp2px(4),ScreenUtils.dp2px(1),ScreenUtils.dp2px(4),ScreenUtils.dp2px(1));
        tvTag.setPadding(ScreenUtils.dp2px(2),ScreenUtils.dp2px(1),ScreenUtils.dp2px(2),ScreenUtils.dp2px(1));
        tvTag.setBackgroundResource(R.drawable.bg_purchase_tag);
        tvTag.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        tvTag.layout(0, 0, tvTag.getMeasuredWidth(), tvTag.getMeasuredHeight());
        tvTag.buildDrawingCache();
        Bitmap bitmap = tvTag.getDrawingCache();
        Drawable d = new BitmapDrawable(bitmap);
        d.setBounds(0, 0, tvTag.getWidth(), tvTag.getHeight());//缺少这句的话，不会报错，但是图片不回显示
        CenterImageSpan span = new CenterImageSpan(d,0, ScreenUtils.dp2px(4));
        spannableString.setSpan(span, 0, strTag.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
        tv.setGravity(Gravity.CENTER_VERTICAL);
    }

    private void initSkuInfo(PurchaseCartProductItemBean dataBean, LinearLayout viewSkuContainer) {
        viewSkuContainer.removeAllViews();
        if (dataBean.getSku_info() != null && dataBean.getSku_info().size() > 0) {
            for (int i = 0; i < dataBean.getSku_info().size(); i++) {
                PurchaseCartItemBean itemBean = dataBean.getSku_info().get(i);
                View view = View.inflate(mContext, R.layout.item_purchase_shop_cart_sku, null);
                View viewCheck = view.findViewById(R.id.item_purchase_cart_selected_layout);
                View viewSku = view.findViewById(R.id.view_sku);
                viewSku.setOnClickListener(v -> {});
                ImageView ivCheck = view.findViewById(R.id.item_purchase_cart_selected_icon);
                TextView tvAttr = view.findViewById(R.id.item_purchase_cart_attr);
                PriceIntegralLayout tvPrice = view.findViewById(R.id.item_purchase_price);
                TextView tvNum = view.findViewById(R.id.item_purchase_cart_num);

                View viewEdit = view.findViewById(R.id.fl_edit);
                View viewSkuReduce = view.findViewById(R.id.view_purchase_sku_reduce);
                View viewSkuAdd = view.findViewById(R.id.view_purchase_sku_plus);
                ImageView dialogPurchaseSkuReduce = view.findViewById(R.id.dialog_purchase_sku_reduce);
                EditText etSkuNum = view.findViewById(R.id.dialog_purchase_sku_num);

                ivCheck.setImageResource(itemBean.getIsSelected() ? R.mipmap.dealer_radio_select : R.mipmap.dealer_radio_default);
                tvAttr.setText(itemBean.getAttribute_text());
                tvNum.setText(String.format("×%s", itemBean.getBuy_qty()));
                tvPrice.setData("0", itemBean.getPrice());
                if (itemBean.getIsEdit()) {
                    viewEdit.setVisibility(View.VISIBLE);
                    tvNum.setVisibility(View.GONE);
                    etSkuNum.setText(itemBean.getBuy_qty());
                    dialogPurchaseSkuReduce.setImageResource(Integer.parseInt(itemBean.getBuy_qty()) <= 1 ? R.mipmap.icon_purchase_grey_sub : R.mipmap.icon_purchase_sub);
                    etSkuNum.setSelection(etSkuNum.getText().toString().length());
                } else {
                    viewEdit.setVisibility(View.GONE);
                    tvNum.setVisibility(View.VISIBLE);
                }

                etSkuNum.setOnClickListener(v -> {
                    etSkuNum.setCursorVisible(true);
                    onPurchaseShopCartListener.onEditClick(itemBean);
                });
                etSkuNum.setOnTouchListener((v, event) -> {
                    etSkuNum.setCursorVisible(true);
                    onPurchaseShopCartListener.onEditClick(itemBean);
                    return false;
                });

                viewCheck.setOnClickListener(v -> {
                    onPurchaseShopCartListener.onSkuGoodsCheck(itemBean.getProduct_main_id(), itemBean.getCart_id(), !itemBean.getIsSelected());
                });
                viewSkuReduce.setOnClickListener(v -> {
                    onPurchaseShopCartListener.onSkuReduce(itemBean);
                });
                viewSkuAdd.setOnClickListener(v -> {
                    onPurchaseShopCartListener.onSkuAdd(itemBean);
                });
                viewSkuContainer.addView(view);
            }
            viewSkuContainer.setVisibility(View.VISIBLE);
        } else {
            viewSkuContainer.setVisibility(View.GONE);
        }
    }
}
